package com.centaline.androidsalesblog.overlay.local;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.AppCompatTextView;
import android.view.LayoutInflater;
import android.view.View;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.base.BaseOverlay;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.iml.BaiduMarkerClick;
import com.centaline.androidsalesblog.model.databaseobject.NewEstDo;
import com.centaline.androidsalesblog.utils.PriceUtil;

import java.util.ArrayList;
import java.util.Locale;

/**
 * 新盘列表地图Overlay
 * Created by guilin on 16/4/13.
 */
public class NewPropMapOverlay extends BaseOverlay<NewEstDo> {

    //选中后的
    private View view, selectView;
    private AppCompatTextView atv_mark_title, atv_select;
    private Overlay selectOverlay;

    public NewPropMapOverlay(Context context, BaiduMap baiduMap, ArrayList<NewEstDo> data,
                             BaiduMarkerClick baiduMarkerClick) {
        super(context, baiduMap, data, baiduMarkerClick);
    }

    @Override
    protected void initView() {
        view = LayoutInflater.from(context).inflate(R.layout.layout_map_new_est, null);
        atv_mark_title = (AppCompatTextView) view.findViewById(R.id.atv_mark_title);

        selectView = LayoutInflater.from(context).inflate(R.layout.layout_map_new_est_xz, null);
        atv_select = (AppCompatTextView) selectView.findViewById(R.id.atv_select);
    }

    /**
     * 把第一个作为中心点
     */
    public void addFirstToMap() {
        /*检查显示条件*/
        if (checkAll())
            return;
        removeFromMap();
        if (data.size() > 0) {
            for (NewEstDo newEstDo : data) {
                if (newEstDo.getLat() != 0 &&
                        newEstDo.getLng() != 0) {
                    overlayList.add(baiduMap.addOverlay(getMarkerOptions(newEstDo)));
                    baiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLngZoom(new LatLng(newEstDo.getLat(), newEstDo.getLng()),
                            15f));
                    break;
                }
            }
        }
    }

    @Override
    public void addToMap(boolean zoomToSpan) {
        /*检查显示条件*/
        if (checkAll())
            return;
        ArrayList<Overlay> tempOverlay = new ArrayList<>();
        ArrayList<String> estExtIds = new ArrayList<>();
        for (Overlay overlay : overlayList) {
            if (overlay instanceof Marker) {
                Marker marker = (Marker) overlay;
                NewEstDo newEstDo = overlay.getExtraInfo().getParcelable(ExtraConstant.MAP_EXTRA_INFO);
                if (newEstDo != null &&
                        baiduMap.getMapStatusLimit().contains(marker.getPosition())) {//已经包含
                    estExtIds.add(newEstDo.getEstExtId());
                    tempOverlay.add(overlay);
                } else {
                    overlay.remove();
                }
            }
        }
        overlayList.clear();
        overlayList.addAll(tempOverlay);
        int size = data.size();
        for (int i = 0; i < size; i++) {
            if (!estExtIds.contains(data.get(i).getEstExtId()) &&
                    isInBound(data.get(i))) {
                overlayList.add(baiduMap.addOverlay(getMarkerOptions(data.get(i))));
            }
        }
        /*全部显示在视野范围内*/
        if (zoomToSpan)
            zoomToSpan();
    }

    /**
     * 添加所有
     */
    public void addAllToMap(boolean zoomToSpan) {
        /*检查显示条件*/
        if (checkAll())
            return;
        for (NewEstDo newEstDo : data) {
            overlayList.add(baiduMap.addOverlay(getMarkerOptions(newEstDo)));
        }
        /*全部显示在视野范围内*/
        if (zoomToSpan)
            zoomToSpan();
    }

    @Override
    public void removeFromMap() {
        super.removeFromMap();
        if (selectOverlay != null) {
            selectOverlay.remove();
        }
    }

    @Override
    public void mapStatusChange() {
        /*检查显示条件*/
        if (checkAll())
            return;
        ArrayList<Overlay> tempOverlay = new ArrayList<>();
        ArrayList<String> estExtIds = new ArrayList<>();
        for (Overlay overlay : overlayList) {
            if (overlay instanceof Marker) {
                Marker marker = (Marker) overlay;
                NewEstDo newEstDo = overlay.getExtraInfo().getParcelable(ExtraConstant.MAP_EXTRA_INFO);
                if (newEstDo != null &&
                        baiduMap.getMapStatusLimit().contains(marker.getPosition())) {//已经包含
                    estExtIds.add(newEstDo.getEstExtId());
                    tempOverlay.add(overlay);
                } else {
                    overlay.remove();
                }
            }
        }
        overlayList.clear();
        overlayList.addAll(tempOverlay);
        int size = data.size();
        for (int i = 0; i < size; i++) {
            if (!estExtIds.contains(data.get(i).getEstExtId()) &&
                    isInBound(data.get(i))) {
                overlayList.add(baiduMap.addOverlay(getMarkerOptions(data.get(i))));
            }
        }
    }

    /**
     * 添加选中后的Overlay
     */
    private void addSelectOverlay(NewEstDo newEstDo) {
        if (selectOverlay != null) {
            selectOverlay.remove();
        }
        Bundle bundle = new Bundle();
        bundle.putParcelable(ExtraConstant.MAP_EXTRA_INFO, newEstDo);
        atv_select.setText(String.format(Locale.CHINA, "%s\n%s",
                newEstDo.getAdName().trim(),
                PriceUtil.newEstAvgPrice(newEstDo.getAveragePrice())));
        selectOverlay = baiduMap.addOverlay(new MarkerOptions()
                .position(new LatLng(newEstDo.getLat(), newEstDo.getLng()))
                .extraInfo(bundle)
                .icon(BitmapDescriptorFactory.fromView(selectView)));
    }

    @Override
    protected OverlayOptions getMarkerOptions(NewEstDo newEstDo) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(ExtraConstant.MAP_EXTRA_INFO, newEstDo);
        atv_mark_title.setText(String.format(Locale.CHINA, "%s\n%s",
                newEstDo.getAdName().trim(),
                PriceUtil.newEstAvgPrice(newEstDo.getAveragePrice())));
        return new MarkerOptions()
                .position(new LatLng(newEstDo.getLat(), newEstDo.getLng()))
                .extraInfo(bundle)
                .icon(BitmapDescriptorFactory.fromView(view));
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        if (overlayList.contains(marker)) {
            if (marker != null) {
                Bundle bundle = marker.getExtraInfo();
                if (bundle != null) {
                    NewEstDo newEstDo = bundle.getParcelable(ExtraConstant.MAP_EXTRA_INFO);
                    if (newEstDo != null) {
                        addSelectOverlay(newEstDo);
                    }
                }
            }
        }
        return baiduMarkerClick.onMarkerClick(marker);
    }

    @Override
    protected boolean isInBound(NewEstDo newEstDo) {
        return baiduMap.getMapStatusLimit().contains(new LatLng(newEstDo.getLat(), newEstDo.getLng()));
    }
}
